package com.acp.service.impl;

import com.acp.dao.acpo.DcsCollectManagementMapper;
import com.acp.dao.acpo.DcsHistoricalAreaMapper;
import com.acp.dao.acpo.DcsHistoricalBuildingMapper;
import com.acp.model.DcsCollectManagement;
import com.acp.model.DcsHistoricalArea;
import com.acp.model.DcsHistoricalBuilding;
import com.acp.param.DcsCollectManagementParam;
import com.acp.result.PageResult;
import com.acp.service.DcsCollectManagementService;
import com.acp.util.UUIDBuild;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.construction
 * @Description: 采集管理实现类
 * @author: 张星亮
 * @date: 2019/4/5
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Repository
@Service
public class DcsCollectManagementServiceImpl implements DcsCollectManagementService {

    private final static String excel2003 = ".xls";
    private final static String excel2007 = ".xlsx";

    @Resource
    private DcsCollectManagementMapper dcsCollectManagementMapper;
    @Resource
    private DcsHistoricalAreaMapper dcsHistoricalAreaMapper;
    @Resource
    private DcsHistoricalBuildingMapper dcsHistoricalBuildingMapper;

    /**
     * 查询
     * @return 返回PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public PageResult selectCollect() {
        List<DcsCollectManagement> dcsCollectManagements = dcsCollectManagementMapper.selectCollect();
        if (dcsCollectManagements.size() > 0) {
            for (int i = 0; i < dcsCollectManagements.size(); i++) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = dcsCollectManagements.get(i).getInput_time();
                String date1 = sdf.format(date);
                dcsCollectManagements.get(i).setTime(date1);
            }
        }
        PageInfo<DcsCollectManagement> pageInfo = new PageInfo<>(dcsCollectManagements);
        Long count = pageInfo.getTotal();
        return PageResult.success(dcsCollectManagements, count);
    }

    /**
     * 导入excel
     * @param in
     * @param fileName
     * @param dcsCollectManagementParam
     * @return 返回list集合
     * @throws Exception
     */
    @Override
    public List<List<Object>> getBankListByExcel(InputStream in, String fileName, DcsCollectManagementParam dcsCollectManagementParam) throws Exception {
        List<List<Object>> list = null;
        Workbook work = this.getWorkbook(in, fileName);
        if (null == work) {
            throw new Exception("创建Excel工作薄为空！");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;
        list = new ArrayList<List<Object>>();
        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if (sheet == null) {
                continue;
            }
            for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if (row == null || row.getFirstCellNum() == j) {
                    continue;
                }
                List<Object> li = new ArrayList<Object>();
                for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                    cell = row.getCell(y);
                    li.add(cell);
                }
                list.add(li);
            }
        }
        work.close();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date1 = new Date();
        String date2 = sdf.format(date1);
        Date date = sdf.parse(date2);
        String status = "未审核";
        String type = dcsCollectManagementParam.getC_type();

        switch (type) {
            case "历史地段":
                for (int i = 0; i < list.size(); i++) {
                    List<Object> lo = list.get(i);
                    String code = String.valueOf(lo.get(0));
                    int a = dcsCollectManagementMapper.selectCollectCode(code);
                    if (a > 0) {

                    } else {
                        DcsCollectManagement dcsCollectManagement = DcsCollectManagement.builder().c_id(UUIDBuild.getUUID()).c_type(type).c_status(status).c_code(code).c_protector(dcsCollectManagementParam.getC_protector()).contact_number(dcsCollectManagementParam.getContact_number()).input_time(date).build();
                        DcsHistoricalArea dcsHistoricalArea = DcsHistoricalArea.builder().id(UUIDBuild.getUUID()).code_historical_area(String.valueOf(lo.get(0))).name(String.valueOf(lo.get(1))).address(String.valueOf(lo.get(2))).feature(String.valueOf(lo.get(3))).wom(String.valueOf(lo.get(4))).protection_scope(String.valueOf(lo.get(5))).introduction(String.valueOf(lo.get(6))).geometry(String.valueOf(lo.get(7))).remark(String.valueOf(lo.get(8))).build();
                        dcsCollectManagementMapper.insert(dcsCollectManagement);
                        dcsHistoricalAreaMapper.insertCollect(dcsHistoricalArea);
                    }
                }
                break;
            case "历史建筑":
                for (int i = 0; i < list.size(); i++) {
                    List<Object> lo = list.get(i);
                    String code = String.valueOf(lo.get(0));
                    DcsCollectManagement dcsCollectManagement = DcsCollectManagement.builder().c_id(UUIDBuild.getUUID()).c_status(status).c_code(code).c_protector(dcsCollectManagementParam.getC_protector()).contact_number(dcsCollectManagementParam.getContact_number()).input_time(date).build();
                    DcsHistoricalBuilding dcsHistoricalBuilding = DcsHistoricalBuilding.builder().id(UUIDBuild.getUUID()).code_historical_building(String.valueOf(lo.get(0))).name(String.valueOf(lo.get(1))).subdistrict(String.valueOf(lo.get(2))).community(String.valueOf(lo.get(3))).address(String.valueOf(lo.get(4))).elevation(String.valueOf(lo.get(5))).height(String.valueOf(lo.get(6))).base_area_of_construction(String.valueOf(lo.get(7))).gross_area(String.valueOf(lo.get(8))).floor(String.valueOf(lo.get(9))).material(String.valueOf(lo.get(10))).structure(String.valueOf(lo.get(11))).dynasty(String.valueOf(lo.get(12))).category(String.valueOf(lo.get(13))).ownername(String.valueOf(lo.get(14))).ownertype(String.valueOf(lo.get(15))).ownership(String.valueOf(lo.get(16))).purpose(String.valueOf(lo.get(17))).individual_buildings_number(String.valueOf(lo.get(18))).using_state(String.valueOf(lo.get(19))).status_description(String.valueOf(lo.get(20))).natural_damage_course(String.valueOf(lo.get(21))).artificial_damage_course(String.valueOf(lo.get(22))).protection_grade(String.valueOf(lo.get(23))).remark(String.valueOf(lo.get(24))).introduction(String.valueOf(lo.get(25))).geometry(String.valueOf(lo.get(26))).build();
                    dcsHistoricalBuildingMapper.insertCollect(dcsHistoricalBuilding);
                    dcsCollectManagementMapper.insert(dcsCollectManagement);
                }
                break;
            default:
                break;
        }
        return list;
    }

    /**
     * 解析文件格式
     * @param inStr
     * @param fileName
     * @return 返回Workbook 封装的返回参数  保存成功msg:906 保存失败msg:911
     * @throws Exception
     */
    @Override
    public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
        Workbook wb = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if (excel2003.equals(fileType)) {
            wb = new HSSFWorkbook(inStr);
        } else if (excel2007.equals(fileType)) {
            wb = new XSSFWorkbook(inStr);
        } else {
            throw new Exception("解析的文件格式有误！");
        }
        return wb;
    }

    /**
     * 查看历史地段
     * @param dcsCollectManagementParam
     * @return 返回PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public PageResult lookCollectArea(DcsCollectManagementParam dcsCollectManagementParam) {
        String type = dcsCollectManagementParam.getC_type();
        String code = dcsCollectManagementParam.getC_code();
        switch (type) {
            case "历史地段":
                List<DcsHistoricalArea> dcsCollectArea = dcsHistoricalAreaMapper.queryCollectArea(code);
                PageInfo<DcsHistoricalArea> pageInfo = new PageInfo<>(dcsCollectArea);
                Long count = pageInfo.getTotal();
                return PageResult.success(dcsCollectArea, count);
            default:
                List<DcsHistoricalBuilding> dcsHistoricalBuildings = dcsHistoricalBuildingMapper.queryCollectBuild(code);
                PageInfo<DcsHistoricalBuilding> pageInfo1 = new PageInfo<>(dcsHistoricalBuildings);
                Long count1 = pageInfo1.getTotal();
                return PageResult.success(dcsHistoricalBuildings, count1);
        }
    }

}
